package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.bean.ausersBean;
import com.bean.dingdanBean;
import com.util.DBUtil;

public class AdminDao {
	//账号的登录 与注册
	//注册
	public static boolean regist(String account,String password) {//传入张号和密码
		Connection conn = DBUtil.getConnectDb();
		String sql ="insert into ausers (account,password) values(?,?)";
		int a = 0;
		
		
		PreparedStatement stm=null;
		boolean sta = false;
		try {
			stm = conn.prepareStatement(sql);
			stm.setString(1, account);
			stm.setString(2, password);
			a = stm.executeUpdate();
			if(a==0) {
				sta = false;
			}else {
				sta=true;
			}
			return sta;
		}catch (SQLException e) {
//			e.printStackTrace();
			return false;
		}
	}
	
	
	
	
	//登录
		public static boolean login(String account,String password) {//传入张号和密码
			Connection conn = DBUtil.getConnectDb();
			String sql ="select*from ausers where account=? and password=?";
			ResultSet rs;
			
			
			PreparedStatement stm=null;
			boolean sta = false;
			try {
				stm = conn.prepareStatement(sql);
				stm.setString(1, account);
				stm.setString(2, password);
				rs = stm.executeQuery();
				if(rs.next()) {
					sta = true;
				}else {
					sta=false;
				}
				return sta;
			}catch (SQLException e) {
//				e.printStackTrace();
				return false;
			}
			

		}
		
		
		public static boolean change(String A,String B) {//更改密码
			Connection conn=DBUtil.getConnectDb();//获取数据库的连接
			String sql = "UPDATE ausers set password=? where account=?";
			int rs;
			PreparedStatement stm = null;
			
			try {
			stm = conn.prepareStatement(sql);
			stm.setString(1,B);
			stm.setString(2,A);

//			boolean a = false;
			rs = stm.executeUpdate();
			if(rs==1){
				return true;
				} else {
				return false;
				} 
			} catch (SQLException e) {
				return false;
			}
		}
		
		
		
		public static ArrayList<ausersBean> userShow() {//查询全部用户信息 存放多个订单信息的集合
			
			ArrayList<ausersBean> tag_Array = new ArrayList<ausersBean>();//建立一个数组集合
			Connection conn=DBUtil.getConnectDb();
			String sql = "select *from ausers WHERE account != 'root' ORDER BY account ";
			PreparedStatement stm = null;
			ResultSet rs = null;
			
			try {
				stm = conn.prepareStatement(sql);
				rs = stm.executeQuery();
				while(rs.next()) {//读取数据库
					ausersBean tag = new ausersBean();//查询商品 存放单个订单信息
					tag.setAccount(rs.getString("account"));
					tag.setPassword(rs.getString("password"));
					tag_Array.add(tag);
				}
				rs.close();
				stm.close();
				
				
				} catch (SQLException e) {
					e.printStackTrace();
				}
			return tag_Array;
		}
		
		
		
		public static ArrayList<ausersBean> userShow(String account) {//查询全部用户信息 存放多个订单信息的集合
			
			ArrayList<ausersBean> tag_Array = new ArrayList<ausersBean>();//建立一个数组集合
			Connection conn=DBUtil.getConnectDb();
			String sql = "select *from ausers WHERE account=?";
			PreparedStatement stm = null;
			ResultSet rs = null;
			
			try {
				stm = conn.prepareStatement(sql);
				stm.setString(1,account);
				rs = stm.executeQuery();
				while(rs.next()) {//读取数据库
					ausersBean tag = new ausersBean();//查询商品 存放单个订单信息
					tag.setAccount(rs.getString("account"));
					tag.setPassword(rs.getString("password"));
					tag_Array.add(tag);
				}
				rs.close();
				stm.close();
				
				
				} catch (SQLException e) {
					e.printStackTrace();
				}
			return tag_Array;
		}
		
		
		public static boolean userDelete(String account) {//注销账户
			Connection conn=DBUtil.getConnectDb();
			String sql = "CALL DeleteUserIfNoPendingOrders(?);";
			int rs;
			PreparedStatement stm = null;
			try {
			stm = conn.prepareStatement(sql);
			stm.setString(1,account);
			
			boolean a=false;
			rs = stm.executeUpdate();
			if(rs==1){
				return true;
				} else {
				return false;
				} 
			} catch (SQLException e) {
				return false;
			}
		}
		
}






